#include "stdafx.h"
#include "CfgUtil.h"

using namespace pb::cfg;

namespace {
    const int           kSizeLength     = 2;
    const unsigned int  kMaxLength      = 0xFFFF;
}

bool
Util::loadLength(std::fstream& in, unsigned int& length)
{
    char bufLength[kSizeLength];
    in.read(bufLength, kSizeLength);
    if (in.eof())
        return false;

    length = 0;
    for (int i = 0; i < kSizeLength; ++i)
    {
        length = (bufLength[i] & 0xFF);

        if (kSizeLength - 1 != i)
            length = length << 8;
    }

    return true;
}

bool
Util::loadName(std::fstream& in, const unsigned int length, std::string& name)
{
    Buffer<char> bufName(length + 1);
    bufName[length] = '\0';
    in.read(bufName.data(), length);
    if (in.eof())
        return false;

    name = bufName.data();
    return true;
}